﻿using System;
using System.Data;
using System.Data.SqlClient;
using DBModule;

namespace DataLogical.TableClass
{
    #region tblConfigClassDefine
    /// <summary>
    /// This object represents the properties and methods of a tblConfigClassDefine.
    /// </summary>

    public class tblConfigClassDefine
    {
        protected int _iD;
        protected string _typeName = String.Empty;
        protected string _typeCName = String.Empty;
        protected string _className = String.Empty;

        public tblConfigClassDefine()
        {
        }

        public tblConfigClassDefine(int ID)
        {
            SqlModule db = CommonVar.s_SqlModule;
            string strSql = string.Format("SELECT ID,TypeName,TypeCName,ClassName FROM tblConfigClassDefine WHERE ID = {0}", ID);
            SqlDataReader reader = db.FastQueryBySql(strSql);
            if (reader.Read())
            {
                this.LoadFromReader(reader);
                reader.Close();
            }
            else
            {
                if (!reader.IsClosed) reader.Close();
                throw new ApplicationException("tblConfigClassDefine does not exist.");
            }
        }

        public tblConfigClassDefine(SqlDataReader reader)
        {
            this.LoadFromReader(reader);
        }

        protected void LoadFromReader(SqlDataReader reader)
        {
            if (reader != null && !reader.IsClosed)
            {
                _iD = reader.GetInt32(0);
                if (!reader.IsDBNull(1)) _typeName = reader.GetString(1);
                if (!reader.IsDBNull(2)) _typeCName = reader.GetString(2);
                if (!reader.IsDBNull(3)) _className = reader.GetString(3);
            }
        }

        public void Delete(int ID)
        {
            SqlModule db = CommonVar.s_SqlModule;
            string strSql = string.Format("Delete From tblConfigClassDefine Where ID={0}", ID);
            db.ExecSql(strSql);
        }

        public DataTable GetAll()
        {
            string strSql = "SELECT ID,TypeName,TypeCName,ClassName FROM tblConfigClassDefine";
            SqlModule db = CommonVar.s_SqlModule;
            return db.QueryBySql(strSql);
        }

        public int Insert(tblConfigClassDefine obj)
        {
            int rtn = 0;
            string strSql = "Insert Into tblConfigClassDefine(TypeName,TypeCName,ClassName)Values(@TypeName,@TypeCName,@ClassName);SELECT @@IDENTITY";
            SqlModule db = CommonVar.s_SqlModule;
            SqlModule.ParameterCollection Params = CreateParams(obj);
            DataTable dt = db.QueryBySql(strSql, Params);
            if (dt.Rows.Count == 1)
            {
                rtn = int.Parse(dt.Rows[0][0] + "");
            }
            return rtn;
        }

        public void Update(tblConfigClassDefine obj)
        {
            string strSql = string.Format("Update tblConfigClassDefine Set TypeName=@TypeName,TypeCName=@TypeCName,ClassName=@ClassName Where ID={0}", obj.ID);
            SqlModule db = CommonVar.s_SqlModule;
            SqlModule.ParameterCollection Params = CreateParams(obj);
            db.ExecSql(strSql, Params);
        }

        private SqlModule.ParameterCollection CreateParams(tblConfigClassDefine obj)
        {
            SqlParameter Param;
            SqlModule.ParameterCollection Result = new SqlModule.ParameterCollection();

            Param = new SqlParameter("@TypeName", SqlDbType.VarChar);
            Result.Add(Param);
            if (obj.TypeName != null)
            {
                Param.Value = obj.TypeName;
            }
            else
            {
                Param.Value = DBNull.Value;
            }
            Param = new SqlParameter("@TypeCName", SqlDbType.VarChar);
            Result.Add(Param);
            if (obj.TypeCName != null)
            {
                Param.Value = obj.TypeCName;
            }
            else
            {
                Param.Value = DBNull.Value;
            }
            Param = new SqlParameter("@ClassName", SqlDbType.VarChar);
            Result.Add(Param);
            if (obj.ClassName != null)
            {
                Param.Value = obj.ClassName;
            }
            else
            {
                Param.Value = DBNull.Value;
            }
            return Result;
        }

        public static bool Exist(int ID)
        {
            SqlModule db = CommonVar.s_SqlModule;
            string strSql = string.Format("SELECT COUNT(*) From tblConfigClassDefine Where ID={0}", ID);
            return (db.ExecSql_Cnt(strSql) == 1);
        }

        #region Public Properties
        public int ID
        {
            get { return _iD; }
        }

        public string TypeName
        {
            get { return _typeName; }
            set { _typeName = value; }
        }

        public string TypeCName
        {
            get { return _typeCName; }
            set { _typeCName = value; }
        }

        public string ClassName
        {
            get { return _className; }
            set { _className = value; }
        }
        #endregion
    }
    #endregion
}
